import { defineStore } from 'pinia'
import { ref } from 'vue'
import { IQuestionnaire, IQuestion } from '@/service/questionnaire/questionnaire.ts'

export const useQuestionnaireStore = defineStore(
	'questionnaire',
	()=>{
		const questionnaires = ref<IQuestionnaire[]>([
			{
				id: 1,
				title: '结核分值量表',
				source: '湖南省胸科医院',
				detail:
					'如分值达到50分及以上需考虑感染结核病，应及时到结核定点医疗机构就诊，分值越高感染结核风险越大。未达到上述分值时不代表无结核病风险，经常规治疗无好转也建议及时进行结核病排查。',
			},
			{
				id: 2,
				title: '结核分值量表',
				source: '湖南省胸科医院',
				detail:
					'如分值达到50分及以上需考虑感染结核病，应及时到结核定点医疗机构就诊，分值越高感染结核风险越大。未达到上述分值时不代表无结核病风险，经常规治疗无好转也建议及时进行结核病排查。',
			},
			{
				id: 3,
				title: '结核分值量表',
				source: '湖南省胸科医院',
				detail:
					'如分值达到50分及以上需考虑感染结核病，应及时到结核定点医疗机构就诊，分值越高感染结核风险越大。未达到上述分值时不代表无结核病风险，经常规治疗无好转也建议及时进行结核病排查。',
			},
		]);
		
		const currentQuestionnaire = ref<IQuestionnaire>();
		
		const questions = ref<IQuestion[]>([
      {
        id: 1,
        title: '年龄',
        type: 'single',
        options: ['5岁及以下', '6岁~30岁', '31岁~64岁', '65岁及以上'],
				scores:[5,0,0,5],
        selectedAnswer: ''
      },
      {
        id: 2,
        title: '以前有无结核病史',
        type: 'single',
        options: ['有', '无'],
				scores:[10,0],
        selectedAnswer: ''
      },
      {
        id: 3,
        title: '共同学习生活者是否有肺结核',
        type: 'single',
        options: ['有，且痰菌阳性', '有，且痰菌阴性', '无'],
				scores:[30,10,0],
        selectedAnswer: ''
      },
      {
        id: 4,
        title: '咳嗽',
        type: 'single',
        options: ['3天及以下', '4天~7天', '8天~14天', '15天及以上'],
				scores:[5,10,20,30],
        selectedAnswer: ''
      },
      {
        id: 5,
        title: '咳血或咯血痰',
        type: 'single',
        options: ['有', '无'],
				scores:[20,0],
        selectedAnswer: ''
      },
      {
        id: 6,
        title: '伴随其他症状',
        type: 'multiple',
        options: ['胸闷、胸痛', '乏力', '食欲下降', '盗汗', '不明原因体重下降', '不明原因低热'],
				scores:[5,5,10,20,20,30],
        selectedAnswer: []
      },
      {
        id: 7,
        title: '合并基础疾病',
        type: 'multiple',
        options: ['恶性肿瘤', '尘肺', '糖尿病', '长期透析', '风湿免疫性疾病', '艾滋病'],
				scores:[15,15,20,20,20,40],
        selectedAnswer: []
      }
    ])
		
		const setCurrentUQuestionnaire = (questionnaire_id: number) => {
			// 确保 questionnaire_id 为数字类型
			const numericQuestionnaireId = Number(questionnaire_id);
			const questionnaire = questionnaires.value.find(a => a.id === numericQuestionnaireId);
			currentQuestionnaire.value = questionnaire; 
		}
		
		const updateAnswer = (questionId: number, answer: string | string[]) => {
			console.log(answer)
			const question = questions.value.find(q => q.id === questionId);
			if (question) {
				question.selectedAnswer = answer;
			}
		}
		
		// 清空用户选择内容
		const clearUserChoices = () => {
			questions.value.forEach(question => {
				if (question.type === 'single') {
					question.selectedAnswer = '';
				} else if (question.type === 'multiple') {
					question.selectedAnswer = [];
				}
			});
		}
		
		// 计算总得分
		const totalScore = computed(() => {
			return questions.value.reduce((total, question) => {
				if (question.type === 'single') {
					// 对于单选题，找到选中的选项的索引，然后获取对应的分数
					const selectedIndex = question.options.indexOf(question.selectedAnswer as string);
					return total + (selectedIndex !== -1 ? question.scores[selectedIndex] : 0);
				} else if (question.type === 'multiple') {
					// 对于多选题，计算每个选中选项的分数之和
					const selectedAnswers = question.selectedAnswer as string[];
					const score = selectedAnswers.reduce((sum, answer) => {
						const index = question.options.indexOf(answer);
						return sum + (index !== -1 ? question.scores[index] : 0);
					}, 0);
					return total + score;
				}
				return total;
			}, 0);
		});
		
		return {
			questionnaires,
			currentQuestionnaire,
			setCurrentUQuestionnaire,
			questions,
			updateAnswer,
			clearUserChoices,
			totalScore
		};
	}
)
